﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Json;

namespace MindMapper.Business
{
    public static class JsonHelper
    {
        public static string Serialize<T>(T entity)
        {
            var memoryStream = new MemoryStream();
            var json = new DataContractJsonSerializer(typeof(T));
            json.WriteObject(memoryStream, entity);

            var streamReader = new StreamReader(memoryStream);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return streamReader.ReadToEnd();
        }

        public static T Deserealize<T>(string entity)
        {
            var memoryStream = new MemoryStream();
            var streamWriter = new StreamWriter(memoryStream);
            streamWriter.Write(entity);
            streamWriter.Flush();

            memoryStream.Seek(0, SeekOrigin.Begin);

            var json = new DataContractJsonSerializer(typeof(T));
            return (T)json.ReadObject(memoryStream);
        }
    }
}
